/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MagnetoProject;

import app.Com;
import app.Parameters;

public class Serialcomm extends Thread {

    static boolean START = false;

    @Override
    public void run(){

        try{
            //*** Open port COM1
            Parameters param = new Parameters();
            param.setPort("COM1");
            param.setBaudRate("38400");
            Com com1 = new Com(param);

            //*** Send four characters every 400 milli seconds
           /*for (int i = 0; i < 4; i++) {
            Thread.sleep(400);
            com1.sendSingleData('G');
            }*/
            com1.sendString("+++", 20);

            Thread.sleep(1000);

           // Receives 'OK'
          for (int i = 0; i < 3; i++) {
            System.out.println(com1.receiveSingleChar());
            }

            Thread.sleep(1000);
            com1.sendString("ATDL7629,MY4287,ID5757,CH19,WR,CN", 20);
            com1.sendSingleData(13);
            for(int i = 0;i<18;i++)
            {
            System.out.println(com1.receiveSingleChar());
            Thread.sleep(100);
            }
            int[] input = new int[3];
            input[2] = 1;
            int xlow, xhigh, ylow, yhigh;
            while(true){
           if( com1.receiveSingleChar() == 'Z')
           {
              xlow = com1.receiveSingleDataInt();//xLow
              xhigh = com1.receiveSingleDataInt();//xHigh
              ylow = com1.receiveSingleDataInt();//yLow
              yhigh = com1.receiveSingleDataInt();//yHigh
              com1.receiveSingleChar();//space char

              input[0] = (xlow + ((xhigh-128)<<8));
              input[1] = (ylow + ((yhigh-128)<<8));

              if(xlow != 0 && ylow != 0 && xhigh != 0 && yhigh != 0)
              Buffer.enter(input);
           }
        }
            
            
            /*while(START){
            for(int i = 0; i<3; i++){
            input[i] = com1.receiveSingleDataInt();
            System.out.println(input[i]);
            Thread.sleep(100);
            }
            Buffer.enter(input);
            
            
            }*/
          //  com1.close();
        }
        catch(Exception e)
        {
            System.out.println("Something went wrong.");
        }
        finally
        {
            //com1.close();
        }
    }

    public void setStart(boolean start){

        START = start;

    }


}

